<template>
  <div>
    <el-card>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        <el-breadcrumb-item>文章视频标签</el-breadcrumb-item>
      </el-breadcrumb>

      <el-button size="mini" icon="el-icon-circle-plus-outline" @click="addPreparer">添加</el-button>
      <el-button size="mini" icon="el-icon-circle-plus-del" @click="delHandle">删除</el-button>

      <el-table :data="tableData" @selection-change="selectChange" border style="width: 100%;margin-top: 10px;"
        height="690" :header-cell-style="{ background: '#409EFF', color: '#FFF' }">
        <el-table-column type="selection"> </el-table-column>
        <el-table-column label="标识" prop="tagId" width="100"> </el-table-column>
        <el-table-column label="名称" prop="tagName"> </el-table-column>
      </el-table>

      <!-- 添加人员弹窗 -->
      <el-dialog title="添加" :visible.sync="addDialogVisible" width="30%" :before-close="closeAddDialog">
        <el-button @click="addName" size="small" style="margin-bottom: 10px;"
          icon="el-icon-circle-plus-outline">添加行</el-button>
        <el-form label-width="80px">
          <el-form-item :label="'标签名' + (index + 1)" v-for="(item, index) in nameArr" :key="index">
            <el-input v-model="item.name" style="margin-bottom: 10px;"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addSubmit">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>



<script>

import {
  addTag, getTagList, delTag
} from "network/content";

export default {
  data() {
    return {
      tableData: [],
      nameArr: [
        { name: '' }
      ],
      addDialogVisible: false,
      ids: '',
    }
  },

  methods: {

    getData() {
      getTagList().then(res => {
        if (res.flag) {
          this.tableData = res.data.reverse()
        }
      })
    },

    delHandle() {
      if (this.ids == '') {
        this.$message.warning('请选择删除的标签')
        return
      }

      delTag({
        tagId: this.ids
      }).then(res => {
        if (res.flag) {
          this.$message.success('删除成功')
          this.getData()
        }
      })

    },

    selectChange(arr) {
      console.log(arr);
      this.ids = ''
      arr.forEach(item => {
        this.ids += item.tagId + ','
      });
    },
    // 添加弹窗
    addPreparer() {
      this.addDialogVisible = true
    },
    addSubmit() {
      console.log(this.nameArr);
      var flag = this.nameArr.some(item => {
        return item.name == ''
      })
      if (flag) {
        return this.$message.warning('请填写完整信息')
      }
      var userNames = ''
      this.nameArr.forEach(item => {
        if (item.name !== '') {
          userNames += item.name + ','
        }
      })

      addTag({
        tagNames: userNames,
      }).then(res => {
        if (res.flag) {
          this.$message.success('添加成功')
          this.addDialogVisible = false
          this.getData()
        }
      })
    },

    addName() {
      this.nameArr.push({
        name: ''
      })
    },
    closeAddDialog() {
      this.nameArr = [
        { name: '' }
      ]
      this.addDialogVisible = false
    },

  },
  created() {
    this.getData()
  },
}



</script>

<style scoped>
.tableTip {
  margin: 0 15px;
  color: red;
  font-size: 12px;
}
</style>